package com.appspot.showup_app.showup;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.appspot.showup_app.showup.model.ShowUpCustomerMessage;
import com.appspot.showup_app.showup.tasks.DeleteCustomerTask;

import java.util.List;

public class CustomersAdapter extends BaseAdapter {

    private static final int EDIT_CUSTOMER_CODE = 0;
    List<ShowUpCustomerMessage> mCustomers;
    Context mContext;

    private LayoutInflater mInflater;

    public CustomersAdapter(Context context, List<ShowUpCustomerMessage> customers) {
        mCustomers = customers;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return mCustomers.size();
    }

    public ShowUpCustomerMessage getItem(int position) {
        return mCustomers.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.customer_list_item, null);

            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();
            // insert into holder all the views you wan to use
            holder.txtName = (TextView) convertView.findViewById(R.id.name);
            holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);
            holder.btnEdit = (ImageButton) convertView.findViewById(R.id.btnEdit);
            holder.btnDelete = (ImageButton) convertView.findViewById(R.id.btnDelete);

            holder.btnEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, EditCustomerActivity.class);
                    intent.putExtra("CUSTOMER_ID", (String) (v.getTag()));
                    ((Activity) mContext).startActivityForResult(intent, EDIT_CUSTOMER_CODE);
                }
            });

            holder.btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(mContext)
                            .setIcon(android.R.drawable.ic_dialog_alert)
                            .setTitle("Delete Client")
                            .setMessage("Are you sure?")
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    final ProgressDialog progressDialog = new ProgressDialog(mContext);
                                    progressDialog.setMessage("Deleting client...");
                                    progressDialog.show();
                                    DeleteCustomerTask deleteCustomerTask = new DeleteCustomerTask(mContext, (String)holder.btnDelete.getTag()) {
                                        @Override
                                        protected void onPostExecute(Void success) {
                                            DatabaseHelper.deleteCustomer(((CustomersActivity)mContext).db, this.mCustomerId);
                                            mCustomers.remove(position);
                                            CustomersAdapter.this.notifyDataSetChanged();
                                            progressDialog.dismiss();
                                        }

                                    };
                                    deleteCustomerTask.execute((Void) null);
                                }
                            })
                            .setNegativeButton("No", null)
                            .show();
                }
            });

            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        ShowUpCustomerMessage customerMessage = getItem(position);
        holder.txtName.setText(customerMessage.getName());
        holder.txtPhone.setText(customerMessage.getPhoneNumber());
        holder.btnEdit.setTag(customerMessage.getId());
        holder.btnDelete.setTag(customerMessage.getId());

        return convertView;
    }


    static class ViewHolder {
        TextView txtName;
        TextView txtPhone;
        ImageButton btnEdit;
        ImageButton btnDelete;
    }

}


